home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / TurboTCP 2.0.1 / TurboTCP source / CTCPStream.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-21  |  10.4 KB  |  282 lines  |  [TEXT/MPCC]

  1. /*
  2. ** CTCPStream.h
  3. **
  4. **    TurboTCP support library
  5. **    TCP stream class
  6. **
  7. **    Copyright © 1993-94, FrostByte Design / Eric Scouten
  8. **
  9. */
  10.  
  11.  
  12. #pragma once
  13.  
  14. #include "TCL.h"
  15.  
  16. #include <Types.h>                            // get <ConditionalMacros.h> if available
  17. #if (defined(GENERATINGPOWERPC) || defined(GENERATING68K))
  18. #include <MacTCP.h>                            // Universal Headers 2.0
  19. #else
  20. #include <MacTCPCommonTypes.h>                // Univ Headers 1.0 or old headers
  21. #include <TCPPB.h>
  22. #define GENERATINGCFM USESROUTINEDESCRIPTORS
  23. #endif
  24.  
  25. #include "CTCPDriver.h"
  26. #include "CTCPEndpoint.h"
  27.  
  28. class CTCPAsyncCall;
  29.  
  30. #ifndef TCL_NO_TEMPLATES
  31.     class CTCPAsyncCallList;
  32.     class CTCPStreamList;
  33. #else
  34.     class CPtrArray_CTCPAsyncCall;
  35.     class CPtrArray_CTCPStream;
  36.     #define CTCPAsyncCallList CPtrArray_CTCPAsyncCall
  37.     #define CTCPStreamList CPtrArray_CTCPStream
  38. #endif
  39.  
  40.  
  41. // connection state codes for TCP Status call
  42.  
  43. enum {
  44.     strClosed = 0,
  45.     strListen = 2,
  46.     strSYNReceived = 4,
  47.     strSYNSent = 6,
  48.     strEstablished = 8,
  49.     strFINWait1 = 10,
  50.     strFINWait2 = 12,
  51.     strCloseWait = 14,
  52.     strClosing = 16,
  53.     strLastAck = 18,
  54.     strTimeWait = 20
  55. };
  56.  
  57.  
  58. // miscellaneous constants
  59.  
  60. #define autoReceiveMax        32                    // maximum # of entries in RDS
  61. #define IPOptionStringSize    40
  62. typedef char IPOptionString[IPOptionStringSize];
  63.  
  64.  
  65. /*______________________________________________________________________
  66. **
  67. ** CTCPStream
  68. **
  69. **    This class implements a stream for MacTCP. A TCP stream supports one connection at
  70. **    a time; but a connection may be closed and another connection opened without releasing
  71. **    the stream.
  72. **
  73. **    All MacTCP commands are originated through this class. (See the methods labelled
  74. **    “basic user TCP calls” below.) These methods originate asynchronous calls to the
  75. **    MacTCP driver by creating CTCPAsyncCall objects to correspond with each call. When
  76. **    the call is completed, notification is passed back to the caller through the Handle…
  77. **    methods in CTCPProtcolInterp.
  78. **
  79. **    This object also receives notification of asynchronous events related to the stream and
  80. **    passes these notifications to its dependents through the BroadcastChange mechanism.
  81. **    (See the description of the asynchronous notification routine [ASR] in the MacTCP
  82. **    manuals.)
  83. **
  84. **    Your application class need not be concerned with interrupt-level constraints on
  85. **    memory management. The CTCPStream object queues notifications received at interrupt
  86. **    level and the CTCPDriver schedules their processing at the next event loop.
  87. **
  88. **    TurboTCP 1.0 NOTE: This class is no longer a descendant of CCollaborator. It now
  89. **    communicates only with the CTCPEndpoint mix-in class.
  90. **
  91. */
  92.  
  93. class CTCPStream TCL_AUTO_DESTRUCT_OBJECT {
  94.  
  95.     friend class CTCPAsyncCall;
  96.     friend class CTCPDriver;
  97.     friend class CTCPEndpoint;
  98.     friend class CTCPStreamList;
  99.  
  100.     TCL_DECLARE_CLASS
  101.  
  102. public:
  103.     ip_addr            itsRemoteIP;                // remote host’s IP address
  104.     b_16                itsRemotePort;                // remote host’s TCP port
  105.     ip_addr            itsLocalIP;                // local host’s IP address
  106.     b_16                itsLocalPort;                // local host’s TCP port
  107.     Boolean            rcvUrgent;                // stream is receiving urgent data
  108.     Boolean            hasSessionOpen;            // currently open session
  109.  
  110. private:
  111.     CTCPEndpoint*        itsEndpoint;                // endpoint object for this stream
  112.     CTCPAsyncCallList* itsAsyncCalls;                // list of outstanding async calls
  113.     Ptr                macTCPStream;            // MacTCP’s reference code for this stream
  114.     Handle            itsBuffer;                    // receive buffer area
  115.     long                itsBufferSize;                // size of receive buffer
  116.  
  117.     b_16                sendNextUrgent;            // next send operation is “urgent” data
  118.     Boolean             sendNextPush;                // next send operation is “push” data
  119.  
  120.     Boolean            notifClosing;                // ASR got closing event
  121.     Boolean             remoteClose;                // ASR close due to remote host
  122.     Boolean            notifTimeout;                // ASR got ULP timeout event
  123.     Boolean             notifTerminate;                // ASR got terminate event
  124.     b_16                notifTermReason;            // ASR’s termination reason
  125.     Boolean            notifDataArrived;            // ASR got data w/ no receive
  126.     Boolean            notifUrgent;                // ASR got urgent data event
  127.     Boolean            notifICMP;                // ASR got ICMP report
  128.     ICMPReport        notifICMPreport;            // most recent ICMP report
  129.     
  130.     char                itsULPtimeout;                // ULP timeout value in seconds
  131.     char                itsULPaction;                // what to do on ULP timeout
  132.     char                itsValidityFlag;                // validity bits for optional parms
  133.     char                itsCommandTimeoutValue;        // command timeout value in seconds
  134.     char                itsTosFlags;                // type of service flags
  135.     char                itsPrecedence;                // precedence level
  136.     char                itsDontFrag;                // don’t fragment flag
  137.     char                itsTimeToLive;                // time to live
  138.     char                itsSecurity;                // security flag
  139.     char                itsOptionCnt;                // count of options bytes
  140.     char                itsOptions[IPOptionStringSize];    // IP options
  141.     
  142.     Boolean             pendingOpen;                // session is being opened
  143.     Boolean             pendingClose;                // session is being closed
  144.     Boolean             pendingAbort;                // session is being aborted
  145.     Boolean            pendingNotify;                // stream is in ProcessNotify queue
  146.     Boolean             pendingDispose;                // stream is in ProcessDispose queue
  147.     Boolean             disposeOnTerminate;            // dispose stream when terminate notification comes
  148.     Boolean            receivedClose;                // received notification that session was closed or aborted
  149.     Boolean            receivedTerminate;            // terminate notification has been received
  150.     
  151.     short            itsAutoReceiveSize;            // auto-receive size (# of entries)
  152.     short            itsAutoReceiveNum;            // number of simultaneous receive calls to issue
  153.     TurboTCPQElem        qNotifyEntry;                // notification queue entry
  154.     TurboTCPQElem        qDisposeEntry;                // disposal queue entry
  155.  
  156. #if GENERATINGCFM
  157.     static UniversalProcPtr notifyProcUPP;            // UPP for asynchronous notification proc
  158. #endif
  159.  
  160.  
  161.     // constructor/destructor
  162.  
  163. private:
  164.                     CTCPStream(CTCPEndpoint& theEndpoint,
  165.                         long recBufferSize = recReceiveSize,
  166.                         short autoReceiveSize = recAutoRecSize,
  167.                         short autoReceiveNum = recAutoRecNum);
  168.     virtual            ~CTCPStream()    ;                    // use Dispose() instead, DO NOT OVERRIDE
  169. public:                
  170.     void                Dispose();
  171.  
  172.     
  173.     // basic user TCP calls
  174.  
  175.     void                OpenConnection(Boolean passive, ip_addr theRemoteIP, b_16 theRemotePort,
  176.                         b_16 theLocalPort);
  177.     void                Close();
  178.     void                Abort();
  179.  
  180.     void                NoCopyRcv(rdsEntry* itsRDS, b_16 itsRDSSize, b_16 itsTimeOut);
  181.     void                BfrReturn(Ptr itsRDS);
  182.     void                Send(wdsEntry* itsWDS, b_16 itsTimeOut, Boolean disposeWDS,
  183.                         Boolean notifyWhenDone);
  184.     void                Receive(Ptr theData, b_16 itsDataSize, b_16 itsTimeOut);
  185.     void                Status(TCPStatusPB* theStatusBlock);
  186.     b_16                ConnectionState();
  187.     inline Boolean        RcvUrgentStatus()
  188.                         { return rcvUrgent; }
  189.  
  190.  
  191.     // specialized functions for sending data
  192.  
  193.     void                SendBfrCpy(const void* theData, unsigned short theDataSize);
  194.     void                SendBfrNoCpy(const void* theData, unsigned short theDataSize,
  195.                         Boolean disposeWhenDone, Boolean notifyWhenDone);
  196.     inline void            SetNextPush()
  197.                         { sendNextPush = TRUE; }
  198.     inline void            SetNextUrgent(Boolean useRFC793)
  199.                         { sendNextUrgent = (useRFC793 ? 2 : 1); }
  200.     
  201.  
  202.     // set configuration — use these before opening a connection
  203.     
  204.     inline void            SetULPTimeoutValue(b_16 ulpTimeoutValue)
  205.                         { itsULPtimeout = ulpTimeoutValue; itsValidityFlag |= timeoutValue; }
  206.     inline void            SetULPTimeoutAction(b_16 ulpTimeoutAction)
  207.                         { itsULPaction = ulpTimeoutAction; itsValidityFlag |= timeoutAction; }
  208.     inline void            SetCommandTimeout(b_16 cmdTimeoutValue)
  209.                         { itsCommandTimeoutValue = cmdTimeoutValue; }
  210.     inline void            SetTypeOfService(b_16 newTosFlag)
  211.                         { itsTosFlags = newTosFlag; itsValidityFlag |= typeOfService; }
  212.     inline void            SetPrecedence(b_16 newPrecedence)
  213.                         { itsPrecedence = newPrecedence; itsValidityFlag |= precedence; }
  214.     inline void            SetDontFrag(b_16 newDontFrag)
  215.                         { itsDontFrag = newDontFrag; }
  216.     inline void            SetTimeToLive(b_16 newTimeToLive)
  217.                         { itsTimeToLive = newTimeToLive; }
  218.     inline void            SetSecurity(b_16 newSecurity)
  219.                         { itsSecurity = newSecurity; }
  220.     inline void            SetIPOptions(b_16 newOptionsSize, IPOptionString* newOptions)
  221.                         { itsOptionCnt = newOptionsSize; BlockMove(newOptions, &itsOptions, IPOptionStringSize); }
  222.     
  223.  
  224.     // notification routines — used by CTCPAsyncCall and CTCPStream *only*
  225.  
  226. private:
  227.     inline void            HandleTCPError(OSErr theResultCode, short theCsCode)
  228.                         { if (itsEndpoint) itsEndpoint->HandleTCPError(theResultCode, theCsCode); }
  229.     inline void            HandleClosed()
  230.                         { if (itsEndpoint) itsEndpoint->HandleClosed(); }
  231.     inline void            HandleClosing(Boolean remoteClosing)
  232.                         { if (itsEndpoint) itsEndpoint->HandleClosing(remoteClosing); }
  233.     inline void            HandleDataArrived(Ptr theData, b_16 theDataSize, Boolean isUrgent)
  234.                         { if (itsEndpoint) itsEndpoint->HandleDataArrived(theData, theDataSize, isUrgent); }
  235.     void                HandleDataSent(wdsEntry* WDSPtr, Boolean disposeWhenDone, Boolean notifyWhenDone);
  236.     inline void            HandleICMP(struct ICMPReport* icmpMsg)
  237.                         { if (itsEndpoint) itsEndpoint->HandleICMP((ICMPType) icmpMsg->reportType,
  238.                             icmpMsg->optionalAddlInfo, (void*) (icmpMsg->optionalAddlInfoPtr)); }
  239.     inline void            HandleOpened()
  240.                         { hasSessionOpen = TRUE; pendingOpen = FALSE; if (itsEndpoint) itsEndpoint->HandleOpened(); }
  241.     inline void            HandleOpenFailed(OSErr theResultCode)
  242.                         { hasSessionOpen = FALSE; pendingOpen = FALSE;
  243.                         if (itsEndpoint) itsEndpoint->HandleOpenFailed(theResultCode); }
  244.     void                HandleSendFailed(wdsEntry* WDSPtr, Boolean disposeWhenDone,
  245.                         Boolean notifyWhenDone, OSErr theResultCode);
  246.     inline void            HandleTerminated(b_16 terminReason)
  247.                         { if (itsEndpoint) itsEndpoint->HandleTerminated((TCPTerminReason) terminReason,
  248.                             pendingDispose || disposeOnTerminate); }
  249.     inline void            HandleTimeout()
  250.                         { if (itsEndpoint) itsEndpoint->HandleTimeout(); }
  251.     inline void            HandleUnexpectedData()
  252.                         { if (itsEndpoint) itsEndpoint->HandleUnexpectedData(); }
  253.     inline void            HandleUrgentBegin()
  254.                         { if (itsEndpoint) itsEndpoint->HandleUrgentBegin(); }
  255.     
  256.     inline void            RcvUrgentBegin()
  257.                         { rcvUrgent = TRUE; }
  258.     inline void            RcvUrgentMark()
  259.                         { rcvUrgent = FALSE; }
  260.  
  261.  
  262.     // private methods for initiating TCP calls
  263.  
  264.     OSErr            DoAsyncCall(b_16 theCsCode, TCPiopb* theParamBlock);
  265.     OSErr            DoSyncCall(b_16 theCsCode, TCPiopb* theParamBlock);
  266.     void                StartAutoReceive()
  267.                         { for (short i=1; i<=itsAutoReceiveNum; i++) IssueAutoReceive(); }
  268.     void                IssueAutoReceive();
  269.     void                ProcessNotify();
  270.     void                ProcessAsyncCompletion(CTCPAsyncCall* theCall);
  271.  
  272.  
  273.     // interrupt-level methods: delay processing for non-interrupt status
  274.  
  275.     void                PostponeDispose();
  276.     void                PostponeNotify(b_16 eventCode, b_16 terminReason, struct ICMPReport* icmpMsg);
  277.     static pascal void    NotifyProc(StreamPtr tcpStream, unsigned short eventCode,
  278.                             Ptr userDataPtr, unsigned short terminReason,
  279.                             struct ICMPReport* icmpMsg);
  280.  
  281. };
  282.